package io.anyways.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import io.anyways.vo.Employee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Slf4j
@Service
public class DemoService {

    @Cacheable(cacheNames = "TEST_Employee", sync = true, key = "#eId", cacheManager = "redisCacheManager")
    public Employee getEmployee(String eId) {
        log.info("加载缓存 - start - {}", eId);
        Employee employee = new Employee();
        employee.setEId(eId);
        employee.setName(RandomUtil.randomString(10));
        employee.setAge(RandomUtil.randomInt(100));
        employee.setBirthday(new Date());
        employee.setCreateTime(LocalDateTime.now());
        try {
            // 模拟查询
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("加载缓存 - end - {}", eId);
        return employee;
    }

    @Cacheable(cacheNames = "REDIS", sync = true, key = "#eId", cacheManager = "simpleCacheManager")
    public String getStringRedisEmployee(String eId) {
        log.info("加载redis缓存 - start - {}", eId);
        Employee employee = new Employee();
        employee.setEId(eId);
        employee.setName(RandomUtil.randomString(10));
        employee.setAge(RandomUtil.randomInt(100));
        employee.setBirthday(new Date());
        employee.setCreateTime(LocalDateTime.now());
        try {
            // 模拟查询
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("加载redis缓存 - end - {}", eId);
        return JSONUtil.toJsonStr(employee);
    }
}
